
package com.be.beadmin.material.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.material.dto.input.PickingDetailsDto;
import com.be.beadmin.material.dto.input.PtCodeAndPage;
import com.be.beadmin.material.dto.output.PickingDetailsEx;
import com.be.beadmin.material.dto.output.PickingTaskEx;
import com.be.beadmin.material.entity.PickingDetails;
import com.be.beadmin.material.entity.PickingDetails_;
import com.be.beadmin.material.entity.PickingTask;
import com.be.beadmin.material.entity.PickingTask_;
import com.be.beadmin.material.mapper.PickingDetailsMapper;
import com.be.beadmin.material.mapper.PickingDetailsMapperEx;
import com.be.beadmin.material.mapper.PickingTaskMapperEx;
import com.be.beadmin.material.repository.PickingDetailsRepository;
import com.be.beadmin.material.repository.PickingReccieveDetailsRepository;
import com.be.beadmin.material.repository.PickingTaskRepository;
import com.be.beadmin.material.service.PickingDetailsService;
import com.be.beadmin.product.service.WorkOrderInforService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import org.springframework.data.domain.PageImpl;


import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* @description 服务实现
* @author zmy
* @date 2023-05-14
**/
@Service
@RequiredArgsConstructor
public class PickingDetailsServiceImpl implements PickingDetailsService {

    @Autowired
    private PickingDetailsRepository pickingDetailsRepository;
    @Autowired
    private PickingDetailsMapper pickingDetailsMapper;

    @Autowired
    private PickingTaskMapperEx pickingTaskExMapper;

    @Autowired
    private PickingTaskRepository pickingTaskRepository;
    @Autowired
    private WorkOrderInforService workOrderInforService;

    @Autowired
    private PickingDetailsMapperEx pickingDetailsExMapper;
    private final PickingReccieveDetailsRepository pickingReccieveDetailsRepository;

    @Override
    public Page<PickingDetails> getPickingDetailsByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<PickingDetails> page = pickingDetailsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(PickingDetails_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(PickingDetails_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
//            Page<PickingDetailsEx> page1 = page.map(pickingDetailsExMapper::toEntity);
            return page;

    }

    public Page<PickingDetailsEx> getPickingDetailsByPageLike(PageEntity pageEntity,String like, String pickingTaskCode){
        Pageable pageable = PageRequest.of(
                pageEntity.getCurrent() -1,
                pageEntity.getPageSize());
        Page<PickingDetails> page = pickingDetailsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(PickingDetails_.delete), 0); //过滤为0
                    list.add(pre2);
//                    Predicate pre3 = criteriaBuilder.equal(root.get(PickingDetails_.pickingTaskCode), pickingTaskCode);
//                    list.add(pre3);
                    Predicate like1 = criteriaBuilder.like(root.get(PickingDetails_.materialCode), "%" + like + "%");
                    Predicate like2 = criteriaBuilder.like(root.get(PickingDetails_.materialSpec), "%" + like + "%");
                    Predicate like3 = criteriaBuilder.like(root.get(PickingDetails_.materialName), "%" + like + "%");
                    Predicate or = criteriaBuilder.or(like1, like3, like2);

                    list.add(or);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(PickingDetails_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                }
                , pageable);
        Page<PickingDetailsEx> page1 = page.map(pickingDetailsExMapper::toEntity);
        return page1;
//        List<PickingDetails> res1 = pickingDetailsRepository.findPickingDetailsByMaterialCodeLikeAndPickingTaskCodeAndDelete("%"+like+"%", pickingTaskCode, 0);
//        List<PickingDetails> res2 = pickingDetailsRepository.findPickingDetailsByPartCodeLikeAndPickingTaskCodeAndDelete("%"+like+"%", pickingTaskCode, 0);
//        List<PickingDetails> res3 = pickingDetailsRepository.findPickingDetailsByMaterialSpecLikeAndPickingTaskCodeAndDelete("%"+like+"%", pickingTaskCode, 0);
//        //List<PickingDetails> res4 = pickingDetailsRepository.findPickingDetailsByMaterialCodeLikeAndDelete("%"+like+"%", 0);
//        List<PickingDetailsEx> ans = new ArrayList<>();
//        for (PickingDetails p :res1) {
//            ans.add(pickingDetailsExMapper.toEntity(p));
//        }
//        for (PickingDetails p :res2) {
//            ans.add(pickingDetailsExMapper.toEntity(p));
//        }
//        for (PickingDetails p :res3) {
//            ans.add(pickingDetailsExMapper.toEntity(p));
//        }
//
//        System.out.println("=========================================");
//        System.out.println(ans.size());
//        PageRequest pageRequest = PageRequest.of(pageEntity.getCurrent(), pageEntity.getPageSize());
//        Page<PickingDetailsEx> page = new PageImpl<>(ans, pageRequest, ans.size());
//
//        return page;
    }

    public Page<PickingDetailsEx> getPickingDetailsByPageLike2(PageEntity pageEntity,String like, String pickingTaskCode) {
        Pageable pageable = PageRequest.of(
                pageEntity.getCurrent() - 1,
                pageEntity.getPageSize());
        Page<PickingDetails> page = pickingDetailsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 = JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(PickingDetails_.delete), 0); //过滤为0
                    list.add(pre2);
//                    Predicate pre3 = criteriaBuilder.equal(root.get(PickingDetails_.pickingTaskCode), pickingTaskCode);
//                    list.add(pre3);
                    Predicate like1 = criteriaBuilder.like(root.get(PickingDetails_.materialCode), "%" + like + "%");
                    Predicate like2 = criteriaBuilder.like(root.get(PickingDetails_.materialSpec), "%" + like + "%");
                    Predicate like3 = criteriaBuilder.like(root.get(PickingDetails_.materialName), "%" + like + "%");
                    Predicate like4 = criteriaBuilder.like(root.get(PickingDetails_.serialNumGroup), "%" + like + "%");
                    Predicate or = criteriaBuilder.or(like1,like2,like3,like4);

                    list.add(or);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if (pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(PickingDetails_.createTime).as(Date.class)));
                    }
                    return criteriaBuilder.and(list.toArray(p));
                }
                , pageable);
        Page<PickingDetailsEx> page1 = page.map(pickingDetailsExMapper::toEntity);
        return page1;
    }
    public List<String> getBarcodesByPickingTaskCode(String pickingTaskCode){
        List<PickingDetails> pds = pickingDetailsRepository.findPickingDetailsByPickingTaskCodeAndDelete(pickingTaskCode, 0);
        LinkedList<String> barcodes = new LinkedList<>();
        for (PickingDetails p:pds) {
            barcodes.add("MaterialCode:"+p.getMaterialCode()+";MaterialSpec:"+p.getMaterialSpec());
        }
        return barcodes;
    }

    @Override
    public List<PickingDetails> getPickingDetailsByBatchPlanId(String batchPlanId) {
        List<PickingTask> pickingTaskByBatchPlanId = pickingTaskRepository.findPickingTaskByBatchPlanIdAndPickingStateAndDelete(batchPlanId, "未分料", 0);
        List<PickingDetails> all = new ArrayList<>();
        for(PickingTask pickingTask : pickingTaskByBatchPlanId){
            List<PickingDetails> pickingDetailsByPickingTaskCodeAndDelete = pickingDetailsRepository.findPickingDetailsByPickingTaskCodeAndDelete(pickingTask.getPickingTaskCode(), 0);
            all.addAll(pickingDetailsByPickingTaskCodeAndDelete);
        }

        List<PickingDetails> res = new ArrayList<>();
        for(PickingDetails p : all){
            boolean flag = true;
            for(PickingDetails r : res){
                if(p.getMaterialCode().equals(r.getMaterialCode())){
                    r.setMaterialQty(r.getMaterialQty() + p.getMaterialQty());
                    r.setOutboundQty(r.getOutboundQty() + p.getOutboundQty());
                    r.setNotIssuedQty(r.getNotIssuedQty() + p.getNotIssuedQty());
                    flag = false;
                    break;
                }
            }
            if(flag){
                PickingDetails r = new PickingDetails();
                r.setMaterialCode(p.getMaterialCode());
                r.setMaterialName(p.getMaterialName());
                r.setMaterialSpec(p.getMaterialSpec());
                r.setIsChanged(p.getIsChanged());
                r.setUnit(p.getUnit());
                r.setPositionNo(p.getPositionNo());
                r.setMaterialQty(r.getMaterialQty() + p.getMaterialQty());
                r.setOutboundQty(r.getOutboundQty() + p.getOutboundQty());
                r.setNotIssuedQty(r.getNotIssuedQty() + p.getNotIssuedQty());
                r.setMaterialType(r.getMaterialType());
                r.setMaterialClassification(r.getMaterialClassification());
                r.setIsJia(p.getIsJia());
                r.setSerialNum(p.getSerialNum());
                r.setCreateTime(p.getCreateTime());
                r.setCreateName(p.getCreateName());
                res.add(r);
            }

        }
        return res;
    }

    @Override
    public void addPickingDetails(PickingDetailsDto pickingDetailsDto) {
            PickingDetails result = pickingDetailsRepository.findPickingDetailsByIdAndDelete(pickingDetailsDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("PickingDetails不存在,请重新查询");
            }
            PickingDetails pickingDetails = pickingDetailsMapper.toEntity(pickingDetailsDto);
            pickingDetailsRepository.save(pickingDetails);
    }

    @Override
    public void updatePickingDetails(PickingDetailsDto pickingDetailsDto) {
            PickingDetails pickingDetails = pickingDetailsRepository.findPickingDetailsById(pickingDetailsDto.getId());
            pickingDetails = pickingDetailsMapper.toEntity(pickingDetailsDto);
            pickingDetailsRepository.save(pickingDetails);
    }


    @Override
    public void delPickingDetails(String id) {
        PickingDetails pickingDetails = pickingDetailsRepository.findPickingDetailsById(id);
        pickingDetails.setDelete(1);
        pickingDetailsRepository.save(pickingDetails);
    }

    @Override
    public void delPickingDetailss (List<String> ids) { }


    /**
     * 分页获取分料细节信息
     *
     * @param ptCodeAndPage
     * @return
     */
    @Override
    public Page<PickingDetailsEx> getPickingDetailsByPageAndPTcode(PtCodeAndPage ptCodeAndPage) {
        String pickTaskCode = ptCodeAndPage.getPickTaskCode();
        PageEntity pageEntity = ptCodeAndPage.getPageEntity();
        Pageable pageable = PageRequest.of(
                pageEntity.getCurrent() -1,
                pageEntity.getPageSize());
        Page<PickingDetails> page = pickingDetailsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(PickingDetails_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate pre3 = criteriaBuilder.equal(root.get(PickingDetails_.pickingTaskCode), pickTaskCode); //过滤为0
                    list.add(pre3);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(PickingDetails_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                }
                , pageable);
        Page<PickingDetailsEx> page1 = page.map(pickingDetailsExMapper::toEntity);
        return page1;
    }

    public void addMaterialQtyByMaterialCodeAndPickingTaskCode(String materialCode,String pickingTaskCode){
        PickingDetails p = pickingDetailsRepository.findPickingDetailsByMaterialCodeAndPickingTaskCodeAndDelete(materialCode, pickingTaskCode,0);
        if(p==null){
            throw new RuntimeException("查询不到该物料！");
        }
        //未传数量，直接填满

        p.setOutboundQty(p.getMaterialQty());
        p.setNotIssuedQty(0);

        pickingDetailsRepository.save(p);

        //如果未领物料的某次分料之后，所有物料都完成了，则改成已分料
        PickingTask pickingTask = pickingTaskRepository.findPickingTaskByPickingTaskCodeAndDelete(pickingTaskCode, 0);
        boolean isFinished = true;
        if(pickingTask.getPickingState().equals("未领物料")){
            List<PickingDetails> pickingDetailsByPickingTaskCodeAndDelete = pickingDetailsRepository.findPickingDetailsByPickingTaskCodeAndDelete(pickingTaskCode, 0);
            for (PickingDetails pickingDetail : pickingDetailsByPickingTaskCodeAndDelete) {
                if(pickingDetail.getNotIssuedQty()!=0) {
                    System.out.println(pickingDetail.getMaterialCode());
                    isFinished=false;
                    break;
                }
            }
            if(isFinished){
                pickingTask.setPickingState("已分料");
                pickingTaskRepository.save(pickingTask);
            }
        }
    }
    @Override
    public void allocation(String id, int num) throws ParseException, BeadminException {
        PickingDetails pickingDetailsById = pickingDetailsRepository.findPickingDetailsById(id);
        String pickingTaskCode=pickingDetailsById.getPickingTaskCode();

        //更新未收数量，已发数量
//        pickingDetailsById.setThistimeQty(num - pickingDetailsById.getOutboundQty() + pickingDetailsById.getThistimeQty());
        if(pickingDetailsById.getThistimeQty() != 0 && (num < 0 || num > pickingDetailsById.getCurrentQty())){
            throw new BeadminException("本次数量不能小于0，并且不能大于未分数量");
        }
        pickingDetailsById.setOutboundQty(num - pickingDetailsById.getThistimeQty() + pickingDetailsById.getOutboundQty());
        pickingDetailsById.setUnclaimedQty(num);
        pickingDetailsById.setThistimeQty(num);
        pickingDetailsById.setNotIssuedQty(pickingDetailsById.getMaterialQty()-num);

        pickingDetailsRepository.save(pickingDetailsById);


        //如果未领物料的某次分料之后，所有物料都完成了，则改成已分料
        PickingTask pickingTask = pickingTaskRepository.findPickingTaskByPickingTaskCodeAndDelete(pickingTaskCode, 0);

        if(pickingTask.getPickingState().equals("未领物料")){
            boolean isFinished = true;
            List<PickingDetails> pickingDetailsByPickingTaskCodeAndDelete = pickingDetailsRepository.findPickingDetailsByPickingTaskCodeAndDelete(pickingTaskCode, 0);
            for (PickingDetails pickingDetail : pickingDetailsByPickingTaskCodeAndDelete) {
                if(pickingDetail.getNotIssuedQty()!=0) {
                    //System.out.println(pickingDetail.getMaterialCode());
                    isFinished=false;
                    break;
                }
            }
            if(isFinished){
                pickingTask.setPickingState("已分料");
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm");
                Date date = new Date();
                pickingTask.setPickingFinishTime(df.parse(df.format(date)));
                pickingTaskRepository.save(pickingTask);
            }
        }


    }



    @Override
    public void allocation2(String pickingdetails_id, double notissueqty, double outboundQty) throws ParseException, BeadminException {
        PickingDetails pickingDetailsById = pickingDetailsRepository.findPickingDetailsById(pickingdetails_id);
        String pickingTaskCode=pickingDetailsById.getPickingTaskCode();

        //更新未收数量，已发数量
//        pickingDetailsById.setThistimeQty(num - pickingDetailsById.getOutboundQty() + pickingDetailsById.getThistimeQty());
//        if(pickingDetailsById.getThistimeQty() != 0 && (num < 0 || num > pickingDetailsById.getCurrentQty())){
//            throw new BeadminException("本次数量不能小于0，并且不能大于未分数量");
//        }
        pickingDetailsById.setOutboundQty(outboundQty);
        pickingDetailsById.setUnclaimedQty(outboundQty);
//        pickingDetailsById.setThistimeQty(num);
        pickingDetailsById.setNotIssuedQty(notissueqty);

        pickingDetailsRepository.save(pickingDetailsById);


        //如果未领物料的某次分料之后，所有物料都完成了，则改成已分料
        PickingTask pickingTask = pickingTaskRepository.findPickingTaskByPickingTaskCodeAndDelete(pickingTaskCode, 0);

        if(pickingTask.getPickingState().equals("未领物料")){
            boolean isFinished = true;
            List<PickingDetails> pickingDetailsByPickingTaskCodeAndDelete = pickingDetailsRepository.findPickingDetailsByPickingTaskCodeAndDelete(pickingTaskCode, 0);
            for (PickingDetails pickingDetail : pickingDetailsByPickingTaskCodeAndDelete) {
                if(pickingDetail.getNotIssuedQty()!=0) {
                    //System.out.println(pickingDetail.getMaterialCode());
                    isFinished=false;
                    break;
                }
            }
            if(isFinished){
                pickingTask.setPickingState("已分料");
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm");
                Date date = new Date();
                pickingTask.setPickingFinishTime(df.parse(df.format(date)));
                pickingTaskRepository.save(pickingTask);
            }
        }


    }

    //TODO
    @Override
    public void allocation3(String batchPlanId, double notissueqty, double outboundQty, String materialCode) throws ParseException {
        List<PickingTask> pickingTaskByBatchPlanId = pickingTaskRepository.findPickingTaskByBatchPlanIdAndPickingStateAndDelete(batchPlanId, "未分料", 0);

        double pre_all_num = 0;
        double this_time_qty = 0;
        for(PickingTask pickingTask : pickingTaskByBatchPlanId){
            List<PickingDetails> pickingDetailsByPickingTaskCodeAndDelete = pickingDetailsRepository.findPickingDetailsByPickingTaskCodeAndDelete(pickingTask.getPickingTaskCode(), 0);
            for(PickingDetails p : pickingDetailsByPickingTaskCodeAndDelete){
                if(p.getMaterialCode().equals(materialCode))
                {
                    pre_all_num = pre_all_num + p.getOutboundQty();
                    this_time_qty = this_time_qty + p.getThistimeQty();
                }
            }
        }

        if(this_time_qty <= outboundQty) {
            outboundQty = outboundQty - pre_all_num;
            for (PickingTask pickingTask : pickingTaskByBatchPlanId) {
                if (outboundQty <= 0) {
                    break;
                }
                List<PickingDetails> pickingDetailsByPickingTaskCodeAndDelete = pickingDetailsRepository.findPickingDetailsByPickingTaskCodeAndDelete(pickingTask.getPickingTaskCode(), 0);
                for (PickingDetails p : pickingDetailsByPickingTaskCodeAndDelete) {
                    if (p.getMaterialCode().equals(materialCode)) {
                        double qty = 0;
                        double notqty = 0;
                        if (outboundQty - p.getNotIssuedQty() > 0) {
                            qty = p.getNotIssuedQty() + p.getOutboundQty();
                            p.setThistimeQty(p.getNotIssuedQty());
                            pickingDetailsRepository.save(p);
                            outboundQty = outboundQty - p.getNotIssuedQty();
                        } else {
                            notqty = p.getNotIssuedQty() - outboundQty;
                            qty = p.getOutboundQty() + outboundQty;
                            p.setThistimeQty(outboundQty);
                            pickingDetailsRepository.save(p);
                            outboundQty = 0;
                        }
                        allocation2(p.getId(), notqty, qty);
                        break;
                    }
                }
            }
        }
        else{
            double minus = this_time_qty - outboundQty;
            for (PickingTask pickingTask : pickingTaskByBatchPlanId) {
                if (minus <= 0) {
                    break;
                }
                List<PickingDetails> pickingDetailsByPickingTaskCodeAndDelete = pickingDetailsRepository.findPickingDetailsByPickingTaskCodeAndDelete(pickingTask.getPickingTaskCode(), 0);
                Collections.reverse(pickingDetailsByPickingTaskCodeAndDelete);
                for (PickingDetails p : pickingDetailsByPickingTaskCodeAndDelete) {
                    if (p.getMaterialCode().equals(materialCode)) {
                        double qty = 0;
                        double notqty = 0;
                        if (minus - p.getThistimeQty() > 0) {
                            qty = p.getOutboundQty() - p.getThistimeQty();
                            notqty = p.getNotIssuedQty() + p.getThistimeQty();
                            p.setThistimeQty(0);
                            pickingDetailsRepository.save(p);
                            minus = minus - p.getThistimeQty();
                        } else {
                            qty = p.getOutboundQty() - minus;
                            minus = 0;
                            notqty = p.getNotIssuedQty() + minus;
                            p.setThistimeQty(p.getThistimeQty() - minus);
                            pickingDetailsRepository.save(p);
                        }
                        allocation2(p.getId(), notqty, qty);
                        break;
                    }
                }
            }
        }

    }

    @Override
    public void scanSerNo(String id,String scanSerNo){
        PickingDetails pickingDetailsById = pickingDetailsRepository.findPickingDetailsById(id);
        if(pickingDetailsById.getSerialNumGroup()==null){
            pickingDetailsById.setSerialNumGroup(scanSerNo);
        }else{
            pickingDetailsById.setSerialNumGroup(pickingDetailsById.getSerialNumGroup()+","+scanSerNo);

        }
        pickingDetailsRepository.save(pickingDetailsById);

    }
}